<template>
    <el-row style="margin-top: 5%;margin-left: 10%">
        <el-form :model="userSecurityData" label-width="150px">
            <el-form-item label="旧密码">
                <el-input v-model="userSecurityData.oldPassword" style="width: 350px"/>
            </el-form-item>
            <el-form-item label="新密码">
                <el-input v-model="userSecurityData.newPassword" style="width: 350px"/>
            </el-form-item>
            <el-form-item label="确认新密码">
                <el-input v-model="userSecurityData.repeatNewPassword" style="width: 350px"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="updatePassword">修改</el-button>
            </el-form-item>
        </el-form>
    </el-row>

</template>


<script>

import {reactive} from "vue";
import router from "@/router";
import StringUtil from "@/utils/StringUtil";
import InfoUtil from "@/utils/InfoUtil";
import RequestUtil from "@/utils/RequestUtil";
import UserUtil from "@/utils/UserUtil";


export default {
    setup() {

        const userSecurityData = reactive({
            oldPassword: "",
            newPassword: "",
            repeatNewPassword: ""
        })

        const updatePassword = () => {
            if (StringUtil.isEmpty(userSecurityData.oldPassword)) {
                InfoUtil.warning("旧密码不能为空")
                return
            }
            if (userSecurityData.oldPassword.length < 8 || userSecurityData.oldPassword.length > 30) {
                InfoUtil.warning("旧密码长度不符合范围")
                return
            }
            if (StringUtil.isEmpty(userSecurityData.newPassword)) {
                InfoUtil.warning("新密码不能为空")
                return
            }

            if (userSecurityData.newPassword.length < 8 || userSecurityData.newPassword.length > 30) {
                InfoUtil.warning("新密码长度不符合范围")
                return
            }
            if (userSecurityData.newPassword === userSecurityData.oldPassword) {
                InfoUtil.warning("新密码不能和旧密码相同")
                return
            }
            if (userSecurityData.repeatNewPassword !== userSecurityData.newPassword) {
                InfoUtil.warning("两次密码不一致")
                return
            }


            const requestData = {
                oldPassword: userSecurityData.oldPassword,
                user: {
                    password: userSecurityData.newPassword
                }
            }
            RequestUtil.sendPostRequest("/user/resetPassword", requestData).then(
                (res) => {
                    debugger
                    if (res.code !== 200) {
                        InfoUtil.warning(res.message);
                        return
                    }
                    InfoUtil.success("你已成功修改密码,即将跳转到登录页")
                    setTimeout(() => {
                        UserUtil.clearToken();
                        router.push('/login');
                    }, 3000);

                },
                (err) => {
                    InfoUtil.error("发送请求失败");
                }
            )


        }
        return {
            userSecurityData,
            updatePassword
        }
    }


}

</script>
<style lang="scss" scoped>
</style>